﻿using Microsoft.VisualBasic;
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams.ExtensionEnablement;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Project.Co_DataTransformationPrototype
{
    public static partial class Operators
    {
        public partial class Operator
        {

            private bool checkRenameOperator<T>(IList<T> elementCollection)
            {
                Debug.Assert(opStore != null);

                // TODO: Implement precondition check
                return true;
            }

            public Operator RenameOperator<T>(IList<T> elementCollection)
            {
                checkRenameOperator(elementCollection);

                if (elementCollection != null)
                {
                    if (elementCollection.OfType<EntityShape>().Count() > 0)
                        renameEntity(elementCollection.OfType<EntityShape>().ToList());
                    else if (elementCollection.OfType<EntityAttribute>().Count() > 0)
                        renameAttribute(elementCollection.OfType<EntityAttribute>().ToList());
                }

                return this;
            }

            private void renameEntity<T>(IList<T> entityList)
            {
                // Transaction is required if you want to update elements.
                using (Transaction t = opStore.TransactionManager.BeginTransaction("Rename Entity"))
                {
                    string strElementName = string.Empty;
                    foreach (T ent in entityList)
                    {
                        Entity element = (ent as EntityShape).ModelElement as Entity;
                        strElementName = Interaction.InputBox("Enter a new element name:", "Rename Element - Inputbox", "ElementName");
                        if (strElementName != string.Empty)
                        {
                            element.Name = strElementName;
                            strElementName = string.Empty; // reset
                        }
                    }
                    t.Commit();
                }
            }

            private void renameAttribute<T>(IList<T> attributeList)
            {
                // Transaction is required if you want to update elements.
                using (Transaction t = opStore.TransactionManager.BeginTransaction("Rename Attribute"))
                {
                    string strAttributeName = string.Empty;
                    foreach (T attribute in attributeList)
                    {
                        strAttributeName = Interaction.InputBox("Enter a new attribute name:", "Rename Attribute - Inputbox", "AttributeName");
                        if (strAttributeName != string.Empty)
                        {
                            (attribute as EntityAttribute).Name = strAttributeName;
                            strAttributeName = string.Empty; // reset
                        }
                    }
                    t.Commit();
                }
            }
        }
    }
}